using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FetCo.Data;
using ADESSA.Business;
using ADESSA.Entities;
using FetCo.Framework;
using FetCo.Web;

public partial class Admin_UnitPrice : System.Web.UI.Page
{
    
  #region Region
    public int NoCountStart
    {
        get { return Convert.ToInt32(ViewState["NoCountStart"]); }
        set { ViewState["NoCountStart"] = value; }
    }
    protected string hidColSort
    {
        get
        {
            object o = ViewState["hidColSort"];
            if (o == null) return "[" + UnitPrice._Table + "].[" + UnitPriceColumns.Order + "]";
            return o.ToString();
        }
        set
        {
            ViewState["hidColSort"] = value;
        }
    }
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
            return;
        LoadCurent();
        LoadData();
    }
    #region LoadCurent
    private void LoadCurent()
    {
        HtmlGenericControl ulUtility = (HtmlGenericControl)Master.FindControl("ulUtility");
        ulUtility.Attributes.Add("class", "current");
        HtmlControl divUtility = (HtmlControl)Master.FindControl("divUtility");
        divUtility.Attributes.Add("class", "select_sub show");
        HtmlGenericControl liCountry = (HtmlGenericControl)Master.FindControl("liCountry");
        liCountry.Attributes.Add("class", "sub_show");

    }
    #endregion
    #region LoadData
    private void LoadData()
    {
        Criteria cri = new Criteria();
        hyperlinkPager.PageSize =10;
        NoCountStart = (hyperlinkPager.PageSize * (hyperlinkPager.CurrentIndex - 1)) + 1;
        int totalrecord = 0;
        //UnitPriceCollection _UnitPriceCollection = CountryService.GetCountryListByParam(CountryColumns.CountryId, OrderDirection.ASC, hyperlinkPager.CurrentIndex, hyperlinkPager.PageSize, cri.Criter, out totalrecord);
        UnitPriceCollection _UnitPriceCollection = UnitPriceService.GetUnitPriceListByParam(hidColSort, OrderDirection.ASC, hyperlinkPager.CurrentIndex, hyperlinkPager.PageSize, cri.Criter, out totalrecord);
        rptList.DataSource = _UnitPriceCollection;
        rptList.DataBind();
        
        //ltrNum.Text = _countryCollection.Count.ToString();
        hyperlinkPager.ItemCount = totalrecord;
        if ((float)totalrecord / (float)hyperlinkPager.PageSize <= 1)
            hyperlinkPager.Visible =true;
    }
    protected void rptList_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            UnitPrice _UnitPrice = (UnitPrice)e.Item.DataItem;
            HiddenField hidID = (HiddenField)e.Item.FindControl("hidID");
            HtmlTableRow tr = (HtmlTableRow)e.Item.FindControl("tr");
            TextBox txtorder = (TextBox)e.Item.FindControl("txtOrder");
            CheckBox chkEnable = (CheckBox)e.Item.FindControl("ckEnable");
            hidID.Value = _UnitPrice.UnitPriceId.ToString();
            txtorder.Text = _UnitPrice.Order.ToString();
            chkEnable.Checked = _UnitPrice.Enable;
            int STT = NoCountStart + e.Item.ItemIndex;
            if (STT % 2 != 0)
            {
                tr.Attributes["class"] = "alternate-row";
            }
            else
            {
                tr.Attributes["class"] = "none";

            }
        }
    }
    protected void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string commandName = e.CommandName;
        object obj = new object();
        switch (commandName)
        {
            case "sortTitle":
                obj = "[" + UnitPrice._Table + "].[" + UnitPriceColumns.Title + "]";
                break;
            case "sortOrder":
                obj = "[" + UnitPrice._Table + "].[" + UnitPriceColumns.Order + "]";
                break;
            default:
                break;
        }
        if (OrderDirection.ASC == OrderDirection.ASC)
            Session["Order"] = OrderDirection.DESC;
        else
            Session["Order"] = OrderDirection.ASC;
        this.hidColSort = obj.ToString();
        LoadData();
    }
    #endregion
    #region Delete UnitPrice
    protected void lndelete_Click(object sender, EventArgs e)
    {
        foreach (RepeaterItem item in this.rptList.Items)
        {
            CheckBox ckbDelete = (CheckBox)item.FindControl("ckbDelete");
            if (ckbDelete.Checked)
            {
                HiddenField hidID = (HiddenField)item.FindControl("hidID");
                string strhidID = hidID.Value;
                long ID = long.Parse(strhidID);
                try
                {
                    #region Check Order data
                    //Criteria cri = new Criteria();
                    //cri.add(ProductSellColumns.CatId, ID, Condition.EQUAL, Condition.AND);
                    //ProductSellCollection _ProductSellCollection = ProductSellService.GetProductSellListByParam(ProductSellColumns.ProductId, OrderDirection.ASC, cri.Criter);
                    //Criteria crit = new Criteria();
                    //crit.add(ProductBuyColumns.CatId, ID, Condition.EQUAL, Condition.AND);
                    //ProductBuyCollection _ProductBuyCollection = ProductBuyService.GetProductBuyListByParam(ProductSellColumns.ProductId, OrderDirection.ASC, crit.Criter);
                    Criteria cr = new Criteria();
                    cr.add(BannerLocationColumns.UnitPriceId,ID, Condition.EQUAL, Condition.AND);
                    BannerLocationCollection _BannerLocationCollection = BannerLocationService.GetBannerLocationListByParam(BannerLocationColumns.UnitPriceId, OrderDirection.ASC, cr.Criter);
                    Criteria cri = new Criteria();
                    cri.add(ProductBuyColumns.PriceRangeUnitId, ID, Condition.EQUAL, Condition.AND);
                    ProductBuyCollection _ProductBuyCollection = ProductBuyService.GetProductBuyListByParam(ProductBuyColumns.ProductId, OrderDirection.ASC, cri.Criter);
                    if ( _BannerLocationCollection.Count > 0 || _ProductBuyCollection.Count>0)
                    {
                        lb_error.Text = "Dont's delete this item because it is getting using another items";
                    }
                    else
                    {
                        UnitPriceService.DeleteUnitPrice(ID);
                    }
                    #endregion
                    
                }
                catch
                {
                    lb_error.Text = "Dont's delete this item because it is getting using another items";
                }
                
                
            }
        }
        LoadData();
    }
    #endregion
    protected void btnSave_OnClick(object sender, EventArgs e)
    {
        foreach (RepeaterItem item in this.rptList.Items)
        {
            HiddenField hidID = (HiddenField)item.FindControl("hidID");
            CheckBox ckEnable = (CheckBox)item.FindControl("ckEnable");
            //UnitPrice _UnitPrice = CountryService.GetCountry(long.Parse(hidID.Value));
            UnitPrice _UnitPrice = UnitPriceService.GetUnitPrice(long.Parse(hidID.Value));
            TextBox txtOrder = (TextBox)item.FindControl("txtOrder");

            if (_UnitPrice != null)
            {
                _UnitPrice.Order = long.Parse(txtOrder.Text.Trim());
                _UnitPrice.Enable = ckEnable.Checked;
                UnitPriceService.UpdateUnitPrice(_UnitPrice);
            }
        }
        LoadData();
    }
    protected void pager_Command(object sender, CommandEventArgs e)
    {
        int currnetPageIndx = Convert.ToInt32(e.CommandArgument);
        hyperlinkPager.CurrentIndex = currnetPageIndx;
        LoadData();
    }
    }

